package com.youlai.boot.system.controller;

import com.youlai.boot.common.excel.ExcelOperationService;
import com.youlai.boot.common.result.ExcelResult;
import com.youlai.boot.system.service.BrandService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.system.model.form.BrandForm;
import com.youlai.boot.system.model.query.BrandQuery;
import com.youlai.boot.system.model.vo.BrandVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;

/**
 * 商品销售品牌数据前端控制层
 *
 * @author xiheforever
 * @since 2025-05-12 23:13
 */
@Tag(name = "商品销售品牌数据接口")
@RestController
@RequestMapping("/api/v1/brand")
@RequiredArgsConstructor
public class BrandController  {

    private final BrandService brandService;
    private final ExcelOperationService excelOperationService;

    @Operation(summary = "商品销售品牌数据分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('system:brand:query')")
    public PageResult<BrandVO> getBrandPage(BrandQuery queryParams ) {
        IPage<BrandVO> result = brandService.getBrandPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增商品销售品牌数据")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('system:brand:add')")
    public Result<Void> saveBrand(@RequestBody @Valid BrandForm formData ) {
        boolean result = brandService.saveBrand(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取商品销售品牌数据表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('system:brand:edit')")
    public Result<BrandForm> getBrandForm(
        @Parameter(description = "商品销售品牌数据ID") @PathVariable Long id
    ) {
        BrandForm formData = brandService.getBrandFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改商品销售品牌数据")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('system:brand:edit')")
    public Result<Void> updateBrand(
            @Parameter(description = "商品销售品牌数据ID") @PathVariable Long id,
            @RequestBody @Validated BrandForm formData
    ) {
        boolean result = brandService.updateBrand(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除商品销售品牌数据")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('system:brand:delete')")
    public Result<Void> deleteBrands(
        @Parameter(description = "商品销售品牌数据ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = brandService.deleteBrands(ids);
        return Result.judge(result);
    }

    @Operation(summary = "导出品牌数据Excel")
    @GetMapping("/export")
    @PreAuthorize("@ss.hasPerm('system:brand:export')")
    public void exportExcel(BrandQuery queryParams, HttpServletResponse response) {
        excelOperationService.exportExcel(queryParams, "brand", response);
    }

    @Operation(summary = "导入品牌数据Excel")
    @PostMapping("/import")
    @PreAuthorize("@ss.hasPerm('system:brand:import')")
    public Result<ExcelResult> importExcel(@RequestParam("file") MultipartFile file) {
        ExcelResult result = excelOperationService.importExcel(file, "brand");
        return Result.success(result);
    }

    @Operation(summary = "下载品牌数据导入模板")
    @GetMapping("/template")
    @PreAuthorize("@ss.hasPerm('system:brand:template')")
    public void downloadTemplate(HttpServletResponse response) {
        excelOperationService.downloadTemplate("brand", response);
    }
}
